package com.health.life;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.myjson.Gson;
import com.health.life.enty.CookBook;
import com.health.life.enty.ShowBookResp;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by tukela on 2015/7/6.
 */
public class ShowDetailActivity extends MyBaseActivity {
    private ImageView imgeview;
    private TextView textview1;
    private TextView textview2;
    private TextView textview3;
    private String id;
    private com.google.myjson.Gson gson=new Gson();
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0x200:
                    String receiveJson= (String) msg.obj;
                    CookBook cookBook = gson.fromJson(receiveJson, ShowBookResp.class).getYi18();
                    if (cookBook!=null)
                    {
                        setViewDatas(cookBook);
                        cookBook.setFid(fid);
                        cookBook.setImg(App.BASE_IMG_URL+cookBook.getImg());
                        setViewDatas(cookBook);
                        try {
                            dbUtils.saveOrUpdate(cookBook);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }

                    break;
                case 0x201:
                    try {
                        CookBook cookBook2=dbUtils.findFirst(Selector.from(CookBook.class).where("id","=",id));
                        if (cookBook2!=null)
                        {
                            setViewDatas(cookBook2);
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
            }
        }
    };
    private DbUtils dbUtils;
    private String fid="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showcb);
        fid=getIntent().getStringExtra("FID");
        id=getIntent().getStringExtra("ID");
        dbUtils= DbUtils.create(this);
        imgeview=(ImageView)findViewById(R.id.image);
        textview1=(TextView)findViewById(R.id.textview1);
        textview2=(TextView)findViewById(R.id.textview2);
        textview3=(TextView)findViewById(R.id.textview3);
        getDataFormNet();
    }


    private void setViewDatas(CookBook listbook){
        textview1.setText(listbook.getTag());
        textview2.setText(listbook.getFood());
        textview3.setText(Html.fromHtml(listbook.getMessage()));
        ImageLoader.getInstance().displayImage(listbook.getImg(),imgeview);
    }

    public void getDataFormNet() {

            HttpUtils client = new HttpUtils();
            client.send(HttpRequest.HttpMethod.GET, App.Detail_URL+id, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                    Message message=handler.obtainMessage();
                    message.what=0x200;
                    message.obj=objectResponseInfo.result;
                    handler.sendMessage(message);
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    handler.sendEmptyMessage(0x201);
                }
            });

        }
    }


